#include <stdio.h>
#include <stdlib.h>
#include "racional.h"

/*
* =================================================
*    Filename:  .c
* Description:
*     Created:  dd/mm/18 hh:mm
* =================================================
*/

struct rac
{
	int num;
	int den;
};

Racional *generate()
{
	Racional *p;
	p = (Racional*) malloc(sizeof(struct rac));
	return p;
}

int set_value(Racional *p, int N, int D)
{
	if(p == NULL || D == 0)
		return 0;
	p->num = N;
	p->den = D;
	return 1;
}

int get_value(Racional *p, int *N, int *D)
{
	if(p == NULL)
		return 0;

	*N = p->num;
	*D = p->den;
	return 1;
}

Racional *sum(Racional *R1, Racional *R2)
{
	Racional *R3;
	R3 = generate();

	if(R3 != NULL)
	{
		R3->num = (R1->num*R2->den) + (R2->num*R1->den);
		R3->den = (R1->den * R2->den);
	}
	return R3;
}

Racional *prod(Racional *R1, Racional *R2)
{
	Racional *R;
	R = generate();

	if(R != NULL)
	{
		R->num = R1->num * R2->num;
		R->den = R1->den * R2->den;
	}
	return R;
}

void delete1(Racional **p)
{
	free(*p); /* Libera a memória  */
	*p = NULL; /* Limpa o ponteiro para racional  */
}

int print_value(Racional *R)
{
	printf("%d/%d", R->num, R->den);
}

